package com.dx.mes.service.spc.service;


import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.ISpcBisChartMainDao;
import com.dx.mes.common.dao.ISpcBisGrpDtDao;
import com.dx.mes.common.dao.ISpcBisGrpMainDao;
import com.dx.mes.common.dao.ISpcRetChartInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.service.spc.tx.Spcgrpmn.SpcgrpmnI;
import com.dx.mes.service.spc.tx.Spcgrpmn.SpcgrpmnIA;
import com.dx.mes.service.spc.tx.Spcgrpmn.SpcgrpmnO;
import com.dx.mes.service.spc.tx.Spcgrpmn.SpcgrpmnOA;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Transactional
@Service("fqpgrpmainService")
public class FqpgrpmainService implements IFqpgrpmainService {
	private LogUtils logUtils;
	
	@Autowired
	private ISpcBisGrpMainDao spcBisGrpMainDao;
	@Autowired
	private ISpcBisGrpDtDao spcBisGrpDtDao;
	@Autowired
	private ISpcBisChartMainDao spcBisChartMainDao;
	@Autowired
	private ISpcRetChartInfoDao spcRetChartInfoDao;
	
	public String subMainProc(String evt_no,String strInMsg){
		AppContext.clear();
		AppContext.setCurrServiceName(FqpgrpmainService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FqpgrpmainService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		String strOutTrx = "";
		Session session = spcBisGrpMainDao.getCrSession();
		
		SpcgrpmnI spcgrpmnI = new SpcgrpmnI();
		SpcgrpmnO spcgrpmnO = new SpcgrpmnO();
		spcgrpmnO.setTrx_id("FQPGRPMN");
		spcgrpmnO.setType_id("O");
		spcgrpmnO.setRtn_code(RETURN_CODE_OK);
		spcgrpmnO.setRtn_mesg("success");
		try{
			spcgrpmnI = JacksonUtil.fromJson(strInMsg, SpcgrpmnI.class);
			char action_flg = spcgrpmnI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   :  queryByCondition(spcgrpmnI, spcgrpmnO)  ; break;
				case ACTION_FLG_ADD     :  addFnc(spcgrpmnI, spcgrpmnO)            ; break;
				case ACTION_FLG_UPDATE  :  updateFnc(spcgrpmnI, spcgrpmnO)         ; break;
				case ACTION_FLG_DELETE  :  deleteFnc(spcgrpmnI, spcgrpmnO)         ; break;
				default                 :  errActionFnc(spcgrpmnO);   
			}
			if( RETURN_CODE_OK.equals(spcgrpmnO.getRtn_code()) ){
				session.flush();
				spcgrpmnO.setRtn_mesg(RETURN_MESG_OK);
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex){
			spcgrpmnO.setRtn_code(RETURN_CODE_UNKNOWN);
			spcgrpmnO.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			strOutTrx = JacksonUtil.toJSONStr(spcgrpmnO);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	public void addFnc(SpcgrpmnI spcgrpmnI, SpcgrpmnO spcgrpmnO)  {
		SpcgrpmnIA iary = spcgrpmnI.getIary().get(0);
		Spc_bis_grp_mainId id = new Spc_bis_grp_mainId();
		
		String hql = "FROM Spc_bis_grp_main where id.col_typ='"+ iary.getCol_typ() +
		             "' and grp_name='"+ iary.getGrp_name() +"'";
		Session session = spcBisGrpMainDao.getCrSession();
		Query query = session.createQuery(hql);
		
		Spc_bis_grp_main query_spc_bis_grp_main = (Spc_bis_grp_main)query.uniqueResult();
		
		id.setCol_typ(iary.getCol_typ());
		if(query_spc_bis_grp_main!=null){
			spcgrpmnO.setRtn_code(E_FQPGRPMN_ALREDY_EXIST+"");
			spcgrpmnO.setRtn_mesg("group name already exist ,please check");
			return ;
		}
		String getMaxGrphql ="select max(id.grp_no)FROM Spc_bis_grp_main";
		Query getMaxquery  = session.createQuery(getMaxGrphql);
		Object object = getMaxquery.uniqueResult();
		int iMaxGrpNo=0;
		if(object!=null){
			iMaxGrpNo = Integer.valueOf(object.toString());
		}
		String sNewGrpNo = Util.comInt2String(iMaxGrpNo+1,6);
		String sSubGrpNo = Util.comInt2String(1,6);
		id.setGrp_no(sNewGrpNo);
		Spc_bis_grp_main spc_bis_grp_main = new Spc_bis_grp_main();
		spc_bis_grp_main.setId(id);
		spc_bis_grp_main.setGrp_name(iary.getGrp_name());
		spc_bis_grp_main.setGrp_dsc(iary.getGrp_dsc());
		spc_bis_grp_main.setEvt_usr(iary.getEvt_usr());
		spc_bis_grp_main.setEvt_timestamp(Util.getCurrentTimestamp());
		spcBisGrpMainDao.save(spc_bis_grp_main);
		
		List<SpcgrpmnOA> oaryList = new ArrayList<SpcgrpmnOA>();
		SpcgrpmnOA oary = new SpcgrpmnOA();
		oary.setCol_typ(spc_bis_grp_main.getId().getCol_typ());
		oary.setGrp_no(spc_bis_grp_main.getId().getGrp_no());
		oary.setGrp_name(spc_bis_grp_main.getGrp_name());
		oary.setGrp_dsc(spc_bis_grp_main.getGrp_dsc());
		oary.setEvt_usr(spc_bis_grp_main.getEvt_usr());
		oary.setEvt_timestamp(spc_bis_grp_main.getEvt_timestamp());
		oaryList.add(oary);
		spcgrpmnO.setOary(oaryList);
		spcgrpmnO.setTbl_cnt(oaryList.size());
		
		spcgrpmnO.setRtn_code(RETURN_CODE_OK);
	}
	public void updateFnc(SpcgrpmnI spcgrpmnI, SpcgrpmnO spcgrpmnO){
		List<SpcgrpmnIA> iaryList = spcgrpmnI.getIary();
		for(SpcgrpmnIA iary: iaryList){
			Spc_bis_grp_mainId id = new Spc_bis_grp_mainId();
			id.setCol_typ(iary.getCol_typ());
			id.setGrp_no(iary.getGrp_no());
			Spc_bis_grp_main spc_bis_grp_main = spcBisGrpMainDao.get(id);
			if(spc_bis_grp_main==null){
				spcgrpmnO.setRtn_code(E_FQPGRPMN_NOT_FOUND_RECORD+"");
				spcgrpmnO.setRtn_mesg("not found main group");
				return ;
			}
			if(iary.getGrp_dsc()!=null){
				spc_bis_grp_main.setGrp_dsc(iary.getGrp_dsc());
			}
			if(iary.getGrp_name()!=null){
				spc_bis_grp_main.setGrp_name(iary.getGrp_name());
			}
			spc_bis_grp_main.setEvt_timestamp(Util.getCurrentTimestamp());
			spc_bis_grp_main.setEvt_usr(iary.getEvt_usr());
			spcBisGrpMainDao.update(spc_bis_grp_main);
		}
		spcgrpmnO.setRtn_code(RETURN_CODE_OK);
	}
	public void deleteFnc(SpcgrpmnI spcgrpmnI, SpcgrpmnO spcgrpmnO){
		List<SpcgrpmnIA> iaryList = spcgrpmnI.getIary();
		for(SpcgrpmnIA iary: iaryList){
			Spc_bis_grp_mainId grpMainId = new Spc_bis_grp_mainId();
			grpMainId.setCol_typ(iary.getCol_typ());
			grpMainId.setGrp_no(iary.getGrp_no());
			Spc_bis_grp_main spc_bis_grp_main = spcBisGrpMainDao.get(grpMainId);
			if(spc_bis_grp_main==null){
				spcgrpmnO.setRtn_code(E_FQPGRPMN_NOT_FOUND_RECORD+"");
				spcgrpmnO.setRtn_mesg("not found main group");
				return ;
			}
			spcBisGrpMainDao.delete(spc_bis_grp_main);
			String hql = "FROM Spc_bis_grp_dt where col_typ_fk='"+iary.getCol_typ() +
			              "' AND grp_no_fk='" +iary.getGrp_no() +"'";
			List<Spc_bis_grp_dt> spcBisGrpDtList =  spcBisGrpDtDao.find(hql);
			for(Spc_bis_grp_dt spc_bis_grp_dt: spcBisGrpDtList){
				spcBisGrpDtDao.delete(spc_bis_grp_dt);
			}
			hql = "FROM Spc_bis_chart_main where col_typ_fk='" +iary.getCol_typ() +
			      "' and grp_no_fk='"+ iary.getGrp_no() + "'";
//			      "' and id.chart_no='"+iary.getc
			List<Spc_bis_chart_main> spcBisChartMainList = spcBisChartMainDao.find(hql);
			for(Spc_bis_chart_main spc_bis_chart_main:spcBisChartMainList){
				spcBisChartMainDao.delete(spc_bis_chart_main);
			}
			hql = "FROM Spc_ret_chart_info where col_typ_fk='" +iary.getCol_typ() +
		      "' and grp_no_fk='"+ iary.getGrp_no() + "'";
			List<Spc_ret_chart_info> spcRetChartInfoList = spcRetChartInfoDao.find(hql);
			for(Spc_ret_chart_info spc_ret_chart_info:spcRetChartInfoList){
				spcRetChartInfoDao.delete(spc_ret_chart_info);
			}
//			Spc_bis_grp_dtId id = new Spc_bis_grp_dtId();
//			id.setCol_typ_fk(iary.getCol_typ_fk());
//			id.setGrp_no_fk(iary.getGrp_no_fk());
//			id.setSub_grp_no(iary.getSub_grp_no());
//			spcBisGrpDtDao.delete(spc_bis_grp_dt);
		}
		spcgrpmnO.setRtn_code(RETURN_CODE_OK);
	}
	public void queryByCondition(SpcgrpmnI spcgrpmnI, SpcgrpmnO spcgrpmnO){
		String hql = "FROM Spc_bis_grp_main where 1=1" ;
		SpcgrpmnIA iary = spcgrpmnI.getIary().get(0);
		if(iary.getCol_typ()!=null){
			hql = hql +" and col_typ='" +iary.getCol_typ() +"'";
		}
		if(iary.getGrp_no()!=null){
			hql = hql +" and grp_no='" +iary.getGrp_no() +"'";
		}
		if(iary.getGrp_name()!=null){
			if(iary.getGrp_name().indexOf('*')==-1){
				hql = hql + " and grp_name = '" + iary.getGrp_name() + "'";
			}else{
				hql = hql + " and grp_name like '" + iary.getGrp_name().replace('*', '%') + "'";
			}
		}
		List<Spc_bis_grp_main> spcBisGrpMainList = spcBisGrpMainDao.find(hql);
		List<SpcgrpmnOA> oaryList = new ArrayList<SpcgrpmnOA>();
		for(Spc_bis_grp_main spc_bis_grp_main :spcBisGrpMainList){
			SpcgrpmnOA oary = new SpcgrpmnOA();
			oary.setCol_typ(spc_bis_grp_main.getId().getCol_typ());
			oary.setGrp_no(spc_bis_grp_main.getId().getGrp_no());
			oary.setGrp_name(spc_bis_grp_main.getGrp_name());
			oary.setGrp_dsc(spc_bis_grp_main.getGrp_dsc());
			oary.setEvt_usr(spc_bis_grp_main.getEvt_usr());
			oary.setEvt_timestamp(spc_bis_grp_main.getEvt_timestamp());
			oaryList.add(oary);
		}
		spcgrpmnO.setOary(oaryList);
		spcgrpmnO.setTbl_cnt(oaryList.size());
		spcgrpmnO.setRtn_code(RETURN_CODE_OK);
	}
           
	public void errActionFnc(SpcgrpmnO spcgrpmnO){
		spcgrpmnO.setRtn_code("0000009");
		spcgrpmnO.setRtn_mesg("Invalid Action_flg");
	}
	
}
